# encoding: UTF-8

from __future__ import print_function, division
import numpy as np
import pandas as pd
import datetime as dt
#import pandas.io.data as web
import matplotlib.finance as mpf
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

def ShowDetailChart(x1, x2, x3):
    """展示图表信息"""
    fig, (ax1, ax2) = plt.subplots(2, sharex=True)
    x1NumPy = np.asarray(x1.items())
    x2NumPy = np.asarray(x2.items())
    x3NumPy = np.asarray(x3.items())
    aaa = [dt.datetime.strptime(i[0],"%Y%m%d") for i in x3.items()]

    arg1 = pd.DataFrame(x1NumPy, columns=['date', 'winTimes'], dtype=int)
    arg2 = pd.DataFrame(x2NumPy, columns=['date', 'loseTimes'], dtype=int)
    arg3 = pd.DataFrame(x3NumPy, columns=['date', 'profit'], dtype=float)

    # pd.to_datetime(arg1['date'])
    arg4 = pd.merge(arg1, arg2, on='date')
    arg4.set_index('date')
    # arg4.plot(ax=ax1)
    ax1.plot(aaa, arg4['winTimes'],'-o', color='r', mfc='red')
    ax1.plot(aaa, arg4['loseTimes'],'-o', color='g', mfc='green')
    # arg4['loseTimes'].plot(ax=ax1, color='g')
    # plt.legend(loc='best')

    # tdf3 = arg3.set_index('date')
    # cdf3 = tdf3['profit']
    # cdf3.plot(label = "profit", ax=ax2, color='b')
    arg3.set_index('date')

    ax2.plot(aaa, arg3['profit'],'-o', color='b', mfc='blue')
    # ax2.plot(arg3['date'], arg3['profit'],'-o', color='b', mfc='blue')

    ax2.set_xlabel(r'date')
    ax1.set_ylabel(r'Daily win and lose count')
    ax1.set_title('win & lose Daily')
    props = font_manager.FontProperties(size=14)
    leg = ax1.legend(loc='best', shadow=True, fancybox=True, prop=props)
    #leg.get_frame().set_alpha(0.5)

    ax2.set_ylabel(r'Profit')
    ax2.set_title('profit Daily')
    props = font_manager.FontProperties(size=14)
    leg = ax2.legend(loc='lower right', shadow=True, fancybox=True, prop=props)
    #leg.get_frame().set_alpha(0.5)

    # mpf.candlestick_ohlc(ax2, arg3, width=0.6)

    ax2.xaxis.set_major_locator(mdates.DayLocator(bymonthday=range(1, 32), interval=1))
    ax2.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
    for label in ax1.xaxis.get_ticklabels():
        label.set_rotation(30)

    for ax in ax1, ax2:
        # fmt = mdates.DateFormatter('%Y-%m-%d')
        # ax.xaxis.set_major_formatter(fmt)
        # ax.xaxis_date()
        ax.grid(True)
        ax.autoscale()

    fig.autofmt_xdate()
    fig.tight_layout()
    # plt.setp(plt.gca().get_xticklabels(), rotation=30)
    plt.show()

    fig.savefig('SPY.png')

if __name__ == '__main__':
    # ShowDetailChart()
    pass

